# -*- coding: utf-8 -*-
import re

import scrapy

from meiwenting.items import MeiwentingItem


class MeiwentingSpiderSpider(scrapy.Spider):
    """
    docstring here
        :param scrapy.Spider: Spider
    """
    name = 'meiwenting_spider'
    allowed_domains = ['www.meiwenting.com']
    start_urls = ['http://www.meiwenting.com/a/jingdian/']

    def parse(self, response):
        for sel in response.xpath('//ul[@class="e2"]/li'):
            item = MeiwentingItem()
            item['title'] = sel.xpath('a/text()').extract_first()
            item['classify'] = sel.xpath(
                'span[@class="info"]/a/text()').extract_first()
            item['date'] = sel.xpath(
                'span[@class="info"]/text()').extract()[1][13:-1]
            item['intro'] = sel.xpath(
                'p[@class="intro"]/text()').extract_first()
            item['link'] = sel.xpath('a/@href').extract_first()
            item['post_id'] = item['link'].split('/')[-1].replace('.html', '')
            item['post_type'] = sel.xpath(
                '//div[@class="place"]/a/text()').extract()[-1]
            yield item
        next_page = response.xpath(
            '//ul[@class="pagelist"]/li[last()-1]/a/@href').extract_first()
        url = response.urljoin(next_page)
        yield scrapy.Request(url=url, callback=self.parse)
